package com.ncloud.resman.mapper;

import java.util.List;
import java.util.Map;

import com.ncloud.resman.conf.EmptyStringIfNull;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.DeviceLocation;

/**
 * Created by shiqiang on 2017/8/14.
 */
@Mapper
public interface DeviceLocationMapper {
    @Insert("insert into res_device_location(device_id, sn_num, room_id, room_name, cabinet_id, cabinet_code, start_num, end_num," +
            " state, remarks,create_user,create_date,update_user,update_date)"
            + " values(#{deviceId},#{snNum},#{roomId},#{roomName},#{cabinetId},#{cabinetCode},#{startNum},#{endNum}," +
            "#{state},#{remarks},#{createUser},#{createDate},#{updateUser},#{updateDate})")
    int insertDeviceLocation(DeviceLocation deviceLocation);
    @Update("update res_device_location set room_id=#{roomId},room_name =#{roomName},cabinet_id =#{cabinetId}," +
            "cabinet_code =#{cabinetCode},start_num =#{startNum},end_num=#{endNum},state=#{state} ," +
            "update_user=#{updateUser},update_date=#{updateDate} where device_id =#{deviceId}")
    int updateDeviceLocation(DeviceLocation deviceLocation);

    /*修改sn号码*/
    @Update("update res_device_location set sn_num=#{0} where device_id =#{1}")
    int updateLocationSnNumByDeviceId(String snNum,Long deviceId);
    /*查询开始U为是否存在*/
    @Select(value = { "select count(1) as num from res_cabinet_use  where  state='1' and cabinet_id= #{0} and use_num = #{1}" })
    int queryLocationUseCount(Long cabinetId,int uNum);
    /*删除*/
    @Delete(value = "delete from res_device_location WHERE device_id = #{0}")
    public int deleteLocation(Long deviceId);

    /*根据sn号查询设位置信息,查询有效的状态*/
    @Select(value = { "select * from res_device_location  where  sn_num=#{0} and state='1'" })
    @Results({ @Result(column = "device_id", property = "deviceId"), @Result(column = "sn_num", property = "snNum"),
            @Result(column = "room_id", property = "roomId"), @Result(column = "room_name", property = "roomName"),
            @Result(column = "cabinet_id", property = "cabinetId"), @Result(column = "cabinet_code", property = "cabinetCode"),
            @Result(column = "start_num", property = "startNum"), @Result(column = "end_num", property = "endNum"),
            @Result(column = "state", property = "state"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    DeviceLocation getLocationBySn(Long snNum);

    /*根据设备id号查询设位置信息,查询有效的状态*/
    @Select(value = { "select * from res_device_location  where  device_id=#{0}" })
    @Results({ @Result(column = "device_id", property = "deviceId"), @Result(column = "sn_num", property = "snNum"),
            @Result(column = "room_id", property = "roomId"), @Result(column = "room_name", property = "roomName"),
            @Result(column = "cabinet_id", property = "cabinetId"), @Result(column = "cabinet_code", property = "cabinetCode"),
            @Result(column = "start_num", property = "startNum"), @Result(column = "end_num", property = "endNum"),
            @Result(column = "state", property = "state"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    DeviceLocation getLocationByDeviceId(Long deviceId);
    /*对位置做更新，需要需要处理使用信息，失效调原来的机柜信息，修改新加机柜信息*/

    @Update("update res_cabinet_use set state='0',device_id= null where cabinet_id =#{0} and device_id =#{1}")
    int updateDeviceLocationInvalid(Long cabinetId,Long deviceId);
    /*可用*/
    @Update("update res_cabinet_use set state='1',device_id=#{2} where cabinet_id =#{0} and use_num =#{1}")
    int updateDeviceLocationUse(Long cabinetId,Integer useNum,Long deviceId);

    @Update(value = "UPDATE res_cabinet_use SET device_id = null, state = '0' "
            + "WHERE device_id=#{0}")
    public int updateCabinetUseStateByDevice(Long deviceId);
    /*获取位置展示列表*/
    @Select(value = { "select a.*,f_get_dict('eqpt_type',b.eqpt_type) as eqptTypeName, b.eqpt_type , f_get_dict('eqpt_state',b.state) as eqptStateName ,"
    		+ " b.state as eqptState,area_id,area_name,country_id,country_name,province_id,province_name,city_id,city_name,addr "
    		+ " from res_device_location a  left JOIN sys_computer_room c  on a.room_id = c.room_id,res_device_base b where a.device_id=b.device_id and  a.sn_num =b.sn_num "
            + " and  (#{0}='-1' or b.eqpt_type = #{0})"
            + " and  (#{1}='-1' or b.state = #{1})"
    })
    @Results({ @Result(javaType = String.class,column = "device_id", property = "deviceId"), @Result(column = "sn_num", property = "snNum"),
            @Result(javaType = String.class,column = "room_id", property = "roomId"), @Result(column = "room_name", property = "roomName"),
            @Result(javaType = String.class,column = "cabinet_id", property = "cabinetId"), @Result(column = "cabinet_code", property = "cabinetCode",typeHandler=EmptyStringIfNull.class),
            @Result(column = "start_num", property = "startNum",typeHandler=EmptyStringIfNull.class), @Result(column = "end_num", property = "endNum",typeHandler=EmptyStringIfNull.class),
            @Result(column = "state", property = "state"),@Result(column = "area_id", property = "areaId",typeHandler=EmptyStringIfNull.class),
            @Result(column = "create_date", property = "createDate"),@Result(column = "area_name", property = "areaName",typeHandler=EmptyStringIfNull.class),
            @Result(column = "update_date", property = "updateDate"),@Result(column = "country_id", property = "countryId",typeHandler=EmptyStringIfNull.class),
            @Result(column = "update_user", property = "updateUser"),@Result(column = "country_name", property = "countryName",typeHandler=EmptyStringIfNull.class),
            @Result(column = "eqpt_type", property = "eqptType"),@Result(column = "province_id", property = "provinceId",typeHandler=EmptyStringIfNull.class),
            @Result(column = "eqptTypeName", property = "eqptTypeName"),@Result(column = "province_name", property = "provinceName",typeHandler=EmptyStringIfNull.class),
            @Result(column = "eqptState", property = "eqptState"),@Result(column = "city_id", property = "cityId",typeHandler=EmptyStringIfNull.class),
            @Result(column = "city_name", property = "cityName"),@Result(column = "addr", property = "addr",typeHandler=EmptyStringIfNull.class),
            @Result(column = "eqptStateName", property = "eqptStateName")})
    List<Map<String,Object>> getDeviceLocationList(String eqptType, String state);
    
    //查看U位是否可用
    @Select(value = {"select state from res_cabinet_use where cabinet_id=#{0} and use_num = #{1}"})
    String getCabinetUseState(String cabinetId,String useNum);
    
    //插入U位
    @Update(value = {"update res_cabinet_use set state='1',device_id=#{0} where cabinet_id=#{1} and use_num=#{2}"})
    int updateCabinetUse(Long deviceId,String cabinetId,String useNum);

    /*获取机柜下，ipmi管理网ip服务器列表*/

    @Select(value = { "select a.device_id,c.ipmi_addr from res_device_location a,res_device_base b ,res_device_server c "
            + " where a.device_id=b.device_id and b.device_id = c.device_id and b.state='1' and  a.cabinet_id=#{0} "
    })
    @Results({
            @Result(javaType = String.class,column = "device_id", property = "deviceId"),
            @Result(column = "ipmi_addr", property = "ipmiAddr")
    })
    List<Map<String,Object>> getDeviceServerIpList(Long cabinetId);
}
